//
//  XJPersonHeaderCell.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/4.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJPersonHeaderCell.h"

@implementation XJPersonHeaderCell

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    
    static NSString * cellID = @"XJPersonHeaderCell";
    XJPersonHeaderCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell==nil) {
        cell = [[XJPersonHeaderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.backgroundColor = WHITE_COLOR;
    }
    return cell;
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupSubviews];
    }
    return self;
    
}

- (UILabel *)xjph_titleLab{
    if (!_xjph_titleLab) {
        UILabel * xjph_titleLab = [UILabel new];
        self.xjph_titleLab = xjph_titleLab;
        [self.contentView addSubview:xjph_titleLab];
    }
    return _xjph_titleLab;
}

- (UIImageView *)xjph_indicator{
    if (!_xjph_indicator) {
        UIImageView * xjph_indicator = [UIImageView new];
        self.xjph_indicator = xjph_indicator;
        [self.contentView addSubview:xjph_indicator];
    }
    return _xjph_indicator;
    
}

- (UIImageView *)xjph_iconImgV{
    
    if (!_xjph_iconImgV) {
        UIImageView * xjph_iconImgV = [UIImageView new];
        self.xjph_iconImgV = xjph_iconImgV;
        [self.contentView addSubview:xjph_iconImgV];
    }
    return _xjph_iconImgV;
}

- (void)setupSubviews{
    
    self.xjph_titleLab.text= @"头像";
    self.xjph_titleLab.font = XJFONTSET_R(14);
    self.xjph_titleLab.textColor = XJCOLOR_RGB(17, 17, 17);
    CGFloat xjph_titleLabW = [UILabel labelWithLabel:self.xjph_titleLab getWidthWithTitle:self.xjph_titleLab.text font:self.xjph_titleLab.font];
    CGFloat xjph_titleLabH = [UILabel labelWithLabel:self.xjph_titleLab getHeightByWidth:xjph_titleLabW title:self.xjph_titleLab.text font:self.xjph_titleLab.font];
    self.xjph_titleLab.sd_layout
    .leftSpaceToView(self.contentView, ADAPTWidth(20))
    .centerYEqualToView(self.contentView)
    .widthIs(xjph_titleLabW)
    .heightIs(xjph_titleLabH);
    
    self.xjph_indicator.image = [UIImage imageNamed:@"direction_right_black"];
    self.xjph_indicator.sd_layout
    .centerYEqualToView(self.contentView)
    .rightSpaceToView(self.contentView, ADAPTWidth(20))
    .widthIs(12)
    .heightIs(12);
    
    // 通过NSUserDefault进行设置:
    // 上传头像之后七牛云会有一个回调。里面有一个图片的url 。保存都NSUserDefault对应的key中。然后刷新界面重新赋值。属性是在外部定义的可以直接在外面进行修改。
    
    self.xjph_iconImgV.image = [UIImage imageNamed:@"default_head"];
    self.xjph_iconImgV.layer.masksToBounds = YES;
    self.xjph_iconImgV.contentMode=UIViewContentModeScaleAspectFill;
    
    self.xjph_iconImgV.sd_layout
    .rightSpaceToView(self.xjph_indicator, ADAPTWidth(10))
    .centerYEqualToView(self.contentView)
    .heightIs(60)
    .widthIs(60);
    self.xjph_iconImgV.sd_cornerRadiusFromWidthRatio=@(0.5);

}


#pragma mark - 修改头像
- (void)modifyXJPersonHeaderCellIconImg {
    UIImage *defalutImg = [UIImage imageNamed:@"default_head"];
    self.xjph_iconImgV.image = defalutImg;
    
    NSString *imgurl = [XJUserModel xj_avator];
    //如果是空的就用默默认头像
    if ([NSString isEmptyString:imgurl]) return;
    
    //覆盖默认头像
    [self.xjph_iconImgV sd_setImageWithURL:[NSURL URLWithString:imgurl]];
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
